﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using IPlugIn;
using System.Reflection;

namespace CSharp
{
    public class PlugInManager
    {
        List<PlugIn> plugIns;
        
        public PlugInManager()
        {
            plugIns = new List<PlugIn>();
        }

        public void LoadPlugIn(string path)
        {
            foreach (var file in Directory.GetFiles(path))
            {
                FileInfo fileInfo = new FileInfo(file);
                if (fileInfo.Extension == ".dll")
                {
                    PlugIn temp = new PlugIn();
                    Assembly asm = Assembly.LoadFile(file);
                    Type[] types = asm.GetTypes();
                    foreach (var type in types)
                    {
                        if (type.GetInterface("IPlugInDemo") != null)
                        {
                            temp._PlugIn = (IPlugIn.IPlugInDemo) Activator.CreateInstance(asm.GetType(type.ToString()));
                            plugIns.Add(temp);
                        }
                    }
                }
            }
        }

        public void ShowPlugIn()
        {
            for (int i = 0; i < plugIns.Count; i++)
            {
                plugIns[i]._PlugIn.Show();
            }
        }
    }
}
